using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml;
using System.Web;
using System.Text.RegularExpressions;

namespace Thinxoft.HttpLib
{
	/// <summary>
	/// Summary description for HTTPResource.
	/// </summary>
    //[Serializable]
	public class HttpResource : MarshalByRefObject, IHttpResource
	{
		Uri uri;
		string query;
		string fileName, extension, fileNameWithoutExtention, localPath, documentPath;
	//	HttpApplicationCollection apps;
        HttpApplication serverInfo;
        byte[] data;
        string offset;
        bool compiled, haspage;
        XmlPage page;
        

        public HttpResource(string resource, HttpApplicationCollection apps)
		{		
			//this.apps = apps;            
     //       Initialize(resource);
		}

        public virtual string ResourceType
        {
            get
            {
                return "http";
            }
        }

        ~HttpResource()
        {
            //if (domain != null)
            //{
            //    if (page != null)
            //        page = null;
            //    //AppDomain.Unload(domain);
            //}
        }

        public void Dispose()
        {
            //if (domain != null)
            //{
            //    if (page != null)
            //        page = null;
            //    AppDomain.Unload(domain);
            //    domain = null;
            //}
        }

        public HttpResource(string resource,ref HttpApplication app)
        {
            this.fileName = Path.GetFileName(resource);
            extension = Path.GetExtension(fileName);
            this.fileNameWithoutExtention = Path.GetFileNameWithoutExtension(fileName);
            this.localPath = resource;
            this.documentPath = resource;
            this.serverInfo = app;
            offset = string.Format("{1}", app.Name, resource.Replace(app.DocumentRoot, app.Name));
            offset = offset.Replace('\\', '/');
            
        }
        public HttpResource(string resource, HttpApplication app)
        {
            this.fileName = Path.GetFileName(resource);
            extension = Path.GetExtension(fileName);
            this.fileNameWithoutExtention = Path.GetFileNameWithoutExtension(fileName);
            this.localPath = resource;
            this.documentPath = resource;
            this.serverInfo = app;
            offset = string.Format("{1}", app.Name, resource.Replace(app.DocumentRoot, app.Name));
            offset = offset.Replace('\\', '/');
        }


        public void SetPageInstance(ref XmlPage page)
        {
            this.haspage = true;
            this.page = page;            
        }


        //void Initialize(string arg)
        //{
        //    string resource = arg;
        //    this.uri = new Uri("http://fakedomain" + resource);
        //    localPath = uri.LocalPath;
        //    HttpApplication app = apps.FindHostApplication(localPath);
        //    this.serverInfo = app;

        //    if (resource == "/")
        //    {
               
        //        //search for a default document to serve as the resource
        //        foreach (string doc in app.DefaultDocuments)
        //        {
        //            if (File.Exists(app.DocumentRoot + "/" + doc))
        //            {
        //                this.uri = new Uri("http://fakedomain/" + doc);
        //            }
        //        }


        //    }
           



        //    query = uri.Query;
        //    fileName = Path.GetFileName(uri.LocalPath);
        //    this.documentPath = app.DocumentRoot + uri.LocalPath;
        //    extension = Path.GetExtension(fileName);
        //    this.fileNameWithoutExtention = Path.GetFileNameWithoutExtension(fileName);
           
        //}

        public void Serialize()
        {
            if (!this.IsXFile)
            {
                FileStream fs = new FileStream(this.LocalPath, FileMode.Open);
                data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Flush();
                fs.Close();
            }
        }

        #region [Properties]

        public bool HasPage
        {
            get
            {
                return this.haspage;
            }
        }

        public XmlPage Page
        {
            get
            {
                return this.page;
            }
        }

        public byte[] Data
        {
            get
            {
                return this.data;
            }
        }

        public bool HasData
        {
            get
            {
                if (data != null)
                    return true;
                else
                    return false;
            }
        }



        public string Offset
        {
            get { return this.offset.ToLower(); }
        }

        public HttpApplication ServerInfo
		{
			get {return this.serverInfo;}
		}

		public bool Exists
		{
			get
			{
                string doc = serverInfo.DocumentRoot + documentPath;
				return File.Exists(documentPath);
			}
		}

		public bool IsXFile
		{
			get
			{
				return extension.ToLower() == ".x";
			}
		}

        public bool Compiled
        {
            get
            {
                return this.compiled;
            }
            set
            {
                compiled = true;
            }
        }

        public bool IsCodeFile
        {
            get
            {
                return extension.ToLower() == ".cs";
            }
        }

        public bool IsASPXFile
		{
			get{return extension.ToLower() == ".aspx";}
		}

		public string Query
		{
			get
			{
				return query.ToLower();
			}
		}

		public string PathAndQuety
		{
			get
			{
				return this.uri.PathAndQuery.ToLower();
			}
		}

		public string DocumentPath
		{
			get{return this.documentPath;}
		}

		public string LocalPath
		{
			get{return this.localPath;}
		}

		public string FileName
		{
			get
			{
				return this.fileName.ToLower();
			}
		}

		public string FileNameWithoutExtension
		{
			get
			{
				return this.fileNameWithoutExtention.ToLower();
			}
		}

		public string FileExtension
		{
			get
			{
				return this.extension.ToLower();
			}
        }
        #endregion
    }
}
